package com.cjack.frenchexcel.controller;

import com.alibaba.fastjson.JSONObject;
import com.cjack.frenchexcel.config.AjaxResult;
import com.cjack.frenchexcel.service.CloudDiskService;
import com.cjack.frenchexcel.service.ConsumerRecordService;
import com.cjack.frenchexcel.service.UserAdminService;
import com.cjack.frenchexcel.util.FileUtils;
import com.cjack.frenchexcel.util.IcePdf;
import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@RestController
@RequestMapping("/file")
public class FileEndpoint extends BaseEndpoint {

    @Autowired
    private ConsumerRecordService consumerRecordService;
    @Autowired
    private CloudDiskService cloudDiskService;
    @Autowired
    UserAdminService service;
    @Value("${file.upload.baseServerDir}")
    String baseServerDir;
    @Value("${file.upload.baseClientDir}")
    String baseClientDir;
    @Value("${file.upload.spelitor}")
    String spelitor;
    @Value("${file.upload.serverUrl}")
    String serverUrl;

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult upload(@RequestParam("file") MultipartFile file, @RequestParam("type") String type) {

        if (file.isEmpty()) {
            return AjaxResult.ERROR();
        }

        String fileName = FileUtils.getFileName(file.getOriginalFilename());
        String dumic = type + spelitor + FileUtils.getDirByDate();//动态的这一截路径
        //服务器保存文件的目录
        String destDir = baseServerDir + dumic;
        //供浏览器客户端访问的目录
        String clientDir = baseClientDir + dumic;
        //先判断当前的目录是否存在
        FileUtils.handleDir(destDir);
        File dest = new File(destDir + spelitor + fileName);
        try {
            file.transferTo(dest);
            JSONObject fileInfo = new JSONObject();
            fileInfo.put("filePath", serverUrl + clientDir + spelitor + fileName);
            return AjaxResult.SUCCESS(fileInfo);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return AjaxResult.ERROR();
    }

    /**
     * 上传pdf，功能和upload方法一致，只是会将pdf转换成图片，并同时返回pdf和图片的路径
     *
     * @param file
     * @param type
     * @return
     */
    @RequestMapping(value = "/uploadPdf", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult uploadPdf(@RequestParam("file") MultipartFile file, @RequestParam("type") String type) {

        if (file.isEmpty()) {
            return AjaxResult.ERROR();
        }
        String pdfFileName = FileUtils.getFileName(file.getOriginalFilename());
        String imgFileName = FileUtils.getFileName("tttt.jpg");
        //如果是发票文件，文件名需要另外处理
        if (type.equals("invoice")) {
            pdfFileName = consumerRecordService.getInvoiceFileName() + ".pdf";
            imgFileName = consumerRecordService.getInvoiceFileName() + ".jpg";
        }
        String dumic = type + spelitor + FileUtils.getDirByDate();//动态的这一截路径
        //服务器保存文件的目录
        String destDir = baseServerDir + dumic;
        //供浏览器客户端访问的目录
        String clientDir = baseClientDir + dumic;
        //先判断当前的目录是否存在
        FileUtils.handleDir(destDir);
        String serverPdfFilePath = destDir + spelitor + pdfFileName;//服务器端存储的pdf文件的地址
        String serverImgFileDir = destDir + spelitor;//服务器端存储的jpg文件的目录
        String clientImgFileDir = clientDir + spelitor;//服务器端存储的jpg文件的目录
        try {
            File dest = new File(serverPdfFilePath);
            file.transferTo(dest);
            String serverImgPaths = IcePdf.pdf2Pic(serverPdfFilePath, serverImgFileDir, serverUrl + clientImgFileDir);//将pdf的内容转换到img里面去
            JSONObject fileInfo = new JSONObject();
            fileInfo.put("pdfFilePath", serverUrl + clientDir + spelitor + pdfFileName);
            fileInfo.put("imgFilePaths", serverImgPaths);
            return AjaxResult.SUCCESS(fileInfo);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (PDFException e) {
            e.printStackTrace();
        } catch (PDFSecurityException e) {
            e.printStackTrace();
        }
        return AjaxResult.ERROR();
    }

    /**
     * 上传pdf，功能和upload方法一致，只是会将pdf转换成图片，并同时返回pdf和图片的路径
     *
     * @param file
     * @param type
     * @return
     */
    @RequestMapping(value = "/uploadCloudFile", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult uploadCloudFile(@RequestParam("file") MultipartFile file, @RequestParam("type") String type) {

        if (file.isEmpty()) {
            return AjaxResult.ERROR();
        }
        String pdfFileName = FileUtils.getFileName(file.getOriginalFilename());

        String dumic = type + spelitor + FileUtils.getDirByDate();//动态的这一截路径
        //服务器保存文件的目录
        String destDir = baseServerDir + dumic;
        //供浏览器客户端访问的目录
        String clientDir = baseClientDir + dumic;
        //先判断当前的目录是否存在
        FileUtils.handleDir(destDir);
        String serverPdfFilePath = destDir + spelitor + pdfFileName;//服务器端存储的pdf文件的地址
        String serverImgFileDir = destDir + spelitor;//服务器端存储的jpg文件的目录
        String clientImgFileDir = clientDir + spelitor;//服务器端存储的jpg文件的目录
        try {
            File dest = new File(serverPdfFilePath);
            file.transferTo(dest);
            String serverImgPaths = IcePdf.pdf2Pic(serverPdfFilePath, serverImgFileDir, serverUrl + clientImgFileDir);//将pdf的内容转换到img里面去
            JSONObject fileInfo = new JSONObject();
            fileInfo.put("pdfFilePath", serverUrl + clientDir + spelitor + pdfFileName);
            fileInfo.put("imgFilePaths", serverImgPaths);
            //提取文件中的数据
            //net.sf.json.JSONObject analyseContent = cloudDiskService.analyseImgText( serverImgPaths);
            //fileInfo.put( "analyseContent", analyseContent);
            return AjaxResult.SUCCESS(fileInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.ERROR();
    }


    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void download(HttpServletResponse response, @RequestParam("filePath") String filePath) {

        if (StringUtils.isEmpty(filePath)) {
            return;
        }
        // 设置信息给客户端不解析
        String type = new MimetypesFileTypeMap().getContentType(filePath);
        // 设置contenttype，即告诉客户端所发送的数据属于什么类型
        response.setHeader("Content-type", type);
        // 读取filename
        String fileName = filePath.split(spelitor)[4];
        // 设置扩展头，当Content-Type 的类型为要下载的类型时 , 这个信息头会告诉浏览器这个文件的名字和类型。
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        try {
            // 发送给客户端的数据
            OutputStream outputStream = response.getOutputStream();
            byte[] buff = new byte[1024];
            BufferedInputStream bis;
            //需要先将 excel的浏览器端访问路径 转化为服务器端访问路径
            filePath = FileUtils.transferClientFileUrl(filePath, baseClientDir, baseServerDir);
            bis = new BufferedInputStream(new FileInputStream(new File(filePath)));
            int i = bis.read(buff);
            while (i != -1) {
                outputStream.write(buff, 0, buff.length);
                outputStream.flush();
                i = bis.read(buff);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
